package panel.downloadZipPanel
{
	import assists.Lang;
	import assists.URI;
	import assists.resource.ResourceLoader;
	
	import flash.display.Sprite;
	import flash.display.Stage;
	
	public class ResourceList extends Sprite
	{
		private var _stage:Stage;
		private var _loader:ResourceLoader;
		private var _lang:Lang;
		private var _uri:URI;
		
		private var resourceList:Array;
		private var boxList:Array;
		
		private var fullBtn1:FullBtnBox;
		private var fullBtn2:FullBtnBox;
		
		private var fullBtnClickFunc:Function;
		
		private var _onShowDetail:Function;
		
		private var _onBack:Function;
		
		public var page2Bool:Boolean = false;
		
		private var chooseThisCity:Function;
		
		public function ResourceList(stage:Stage, loader:ResourceLoader, lang:Lang, uri:URI)
		{
			_stage = stage;
			_loader = loader;
			_lang = lang;
			_uri = uri;
			
			resourceList = [];
			boxList = [];
		}
		
		public function changeData(list:Array,page:int):void
		{
			resourceList = list;
			
			if(page == 1){
				showFirstPage();
			}
			if(page == 2){
				showSecondPage();
			}
		}
		
		private function showSecondPage():void
		{
			fullBtn2 = new FullBtnBox(_stage,_loader,_lang,_uri);
			fullBtn2.changeData(2,_onBack,"点击返回城市资源列表");
			this.addChild(fullBtn2);
			
			boxList = [];
			
			var cityBox:DownloadReousrceScenicBox;
			for(var i:int = 0;i < resourceList.length;i++){
				cityBox = new DownloadReousrceScenicBox(_stage,_loader,_lang,_uri);
				
				cityBox.changeData(resourceList[i],page2Bool);
				cityBox.y = fullBtn2.height + cityBox.height * i;
				
				boxList.push(cityBox);
				this.addChild(cityBox);
			}
		}
		
		private function showFirstPage():void
		{
			fullBtn1 = new FullBtnBox(_stage,_loader,_lang,_uri);
			fullBtn1.changeData(1,fullBtnClickFunc,"可下载该城市离线数据");
			this.addChild(fullBtn1);
			
			boxList = [];
			
			var cityBox:DownloadResourceCityBox;
			for(var i:int = 0;i < resourceList.length;i++){
				cityBox = new DownloadResourceCityBox(_stage,_loader,_lang,_uri);
				cityBox.onShowDetail = _onShowDetail;
				cityBox.onChooseCity = chooseThisCity;
				
				cityBox.changeData(resourceList[i]);
				cityBox.y = fullBtn1.height + cityBox.height * i;
				
				boxList.push(cityBox);
				this.addChild(cityBox);
			}
		}
		
		public function set fullBtnClick(func:Function):void
		{
			fullBtnClickFunc = func;
		}
		
		public function set onShowDetail(func:Function):void
		{
			_onShowDetail = func;
		}
		
		public function set backToCity(func:Function):void
		{
			_onBack = func;
		}
		
		public function set onChooseCity(func:Function):void
		{
			chooseThisCity = func;
		}
		
		public function clear():void
		{
			if(_stage){
				_stage = null;
				_loader = null;
				_lang = null;
				_uri = null;
				
				if(fullBtn1){
					fullBtn1.clear();
					fullBtn1 = null;
				}
				
				if(fullBtn2){
					fullBtn2.clear();
					fullBtn2 = null;
				}
				
				for(var i:int = 0;i < boxList.length;i++){
					boxList[i].clear();
				}
			}
		}
	}
}